package com.example.demo.controller.impor;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.File;
public class XmlPaser {
    public static void main(String[] args) {
        try {
            // 创建 DocumentBuilderFactory 实例
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            // 创建 DocumentBuilder 实例
            DocumentBuilder builder = factory.newDocumentBuilder();
            // 解析 XML 文件
            File xmlFile = new File("test2.xml");
            Document document = builder.parse(xmlFile);
            // 规范化文档
            document.getDocumentElement().normalize();

            // 获取根元素
            Element root = document.getDocumentElement();
            System.out.println("Root element: " + root.getNodeName());

            // 处理子元素，例如获取 token 元素的值
            NodeList tokenList = root.getElementsByTagName("token");
            if (tokenList.getLength() > 0) {
                Node tokenNode = tokenList.item(0);
                if (tokenNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element tokenElement = (Element) tokenNode;
                    System.out.println("Token: " + tokenElement.getTextContent());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
